//
//  RctButton.m
//  strechImageView
//
//  Created by VD-Dragon on 2018/1/21.
//  Copyright © 2018年 VD-Dragon. All rights reserved.
//

#import "RctButton.h"
#import "UIView+Extension.h"
@interface RctButton()

@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIImageView *imgv;

@end

@implementation RctButton
- (instancetype)initWithFrame:(CGRect)frame string:(NSString *)string
{
    self = [super initWithFrame:frame];
    if (self) {
//        CGSize ss = [self getAttributeSizeWithText:string fontSize:14];
//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ss.width+25+20, 35)];
//        _selfH = ss.width + 45;
//        CGFloat cornerRadius = 15;
//        [self .view addSubview:view];
        _imgv = [UIImageView new];
        _imgv.frame = CGRectMake(3, 5, 20, 20);
        _imgv.image = [UIImage imageNamed:@"icon通过"];
        [self addSubview:_imgv];
        _label = [UILabel new];
//        _label.frame = CGRectMake(CGRectGetMaxX(imgv.frame)+10, 3, ss.width, 25);
        _label.textColor = [UIColor redColor];
        _label.font = [UIFont systemFontOfSize:14];
        _label.text = @"";
        [self addSubview:_label];
        CGSize ss = [self getAttributeSizeWithText:string fontSize:14];
        self.width = ss.width+45;
        _label.frame = CGRectMake(CGRectGetMaxX(_imgv.frame)+10, 3, ss.width, 25);
        _label.text = string;
        CGFloat viewWidth = CGRectGetWidth(self.frame);
        CGFloat viewHeight = CGRectGetHeight(self.frame);
        CGFloat cornerRadius = 15;
        
        CGFloat arrowX = 55.;
        CGFloat arrowY = 7.;
        CGFloat arrowLength = 10.;
        CGPoint point1 = CGPointMake(0, 0);
        CGPoint point2 = CGPointMake(viewWidth, 0);
        CGPoint point3 = CGPointMake(viewWidth, viewHeight - arrowY);
        //key point 4,5,6
        CGPoint point4 = CGPointMake(arrowX + arrowLength, viewHeight - arrowY);
        CGPoint point5 = CGPointMake(arrowX + arrowLength/2.0, viewHeight);
        CGPoint point6 = CGPointMake(arrowX, viewHeight - arrowY);
        
        CGPoint point7 = CGPointMake(0, viewHeight - arrowY);
        UIBezierPath *path = [UIBezierPath bezierPath];
        
        if (cornerRadius == 0) {
            [path moveToPoint:point1];
            [path addLineToPoint:point2];
            [path addLineToPoint:point3];
            [path addLineToPoint:point4];
            [path addLineToPoint:point5];
            [path addLineToPoint:point6];
            [path addLineToPoint:point7];
            
        }else{
            //顺序有影响
            [path addArcWithCenter:CGPointMake(cornerRadius, cornerRadius) radius:cornerRadius startAngle:2*M_PI_2 endAngle:3*M_PI_2 clockwise:YES];
            [path addArcWithCenter:CGPointMake(viewWidth-cornerRadius, cornerRadius) radius:cornerRadius startAngle:3*M_PI_2 endAngle:0 clockwise:YES];
            [path addArcWithCenter:CGPointMake(viewWidth-cornerRadius, viewHeight-cornerRadius-arrowY) radius:cornerRadius startAngle:0 endAngle:M_PI_2 clockwise:YES];
            [path addLineToPoint:point4];
            [path addLineToPoint:point5];
            [path addLineToPoint:point6];
            [path addArcWithCenter:CGPointMake(cornerRadius, viewHeight-cornerRadius-arrowY) radius:cornerRadius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
        }
        
        [path closePath];
        
        CAShapeLayer *shapeLayer   = [CAShapeLayer layer];
        shapeLayer = [CAShapeLayer layer];
        shapeLayer.path            = path.CGPath;
        
        CAShapeLayer *borderLayer  = [CAShapeLayer layer];
        borderLayer.path           =   path.CGPath;
        borderLayer.fillColor      = [UIColor clearColor].CGColor;
        borderLayer.strokeColor    = [UIColor redColor].CGColor;
        borderLayer.lineWidth      = 2;
        borderLayer.frame          =self.bounds;
        self.layer.mask            = shapeLayer;
        [self.layer addSublayer:borderLayer];
        
    }
    return self;
}

//- (void)setStatus_string:(NSString *)status_string{
//
//    _status_string = status_string;
//    CGSize ss = [self getAttributeSizeWithText:status_string fontSize:14];
//    self.width = ss.width+45;
//    _label.frame = CGRectMake(CGRectGetMaxX(_imgv.frame)+10, 3, ss.width, 25);
//    _label.text = status_string;
//    CGFloat viewWidth = CGRectGetWidth(self.frame);
//    CGFloat viewHeight = CGRectGetHeight(self.frame);
//    CGFloat cornerRadius = 15;
//
//    CGFloat arrowX = 55.;
//    CGFloat arrowY = 7.;
//    CGFloat arrowLength = 10.;
//    CGPoint point1 = CGPointMake(0, 0);
//    CGPoint point2 = CGPointMake(viewWidth, 0);
//    CGPoint point3 = CGPointMake(viewWidth, viewHeight - arrowY);
//    //key point 4,5,6
//    CGPoint point4 = CGPointMake(arrowX + arrowLength, viewHeight - arrowY);
//    CGPoint point5 = CGPointMake(arrowX + arrowLength/2.0, viewHeight);
//    CGPoint point6 = CGPointMake(arrowX, viewHeight - arrowY);
//
//    CGPoint point7 = CGPointMake(0, viewHeight - arrowY);
//    UIBezierPath *path = [UIBezierPath bezierPath];
//
//    if (cornerRadius == 0) {
//        [path moveToPoint:point1];
//        [path addLineToPoint:point2];
//        [path addLineToPoint:point3];
//        [path addLineToPoint:point4];
//        [path addLineToPoint:point5];
//        [path addLineToPoint:point6];
//        [path addLineToPoint:point7];
//
//    }else{
//        //顺序有影响
//        [path addArcWithCenter:CGPointMake(cornerRadius, cornerRadius) radius:cornerRadius startAngle:2*M_PI_2 endAngle:3*M_PI_2 clockwise:YES];
//        [path addArcWithCenter:CGPointMake(viewWidth-cornerRadius, cornerRadius) radius:cornerRadius startAngle:3*M_PI_2 endAngle:0 clockwise:YES];
//        [path addArcWithCenter:CGPointMake(viewWidth-cornerRadius, viewHeight-cornerRadius-arrowY) radius:cornerRadius startAngle:0 endAngle:M_PI_2 clockwise:YES];
//        [path addLineToPoint:point4];
//        [path addLineToPoint:point5];
//        [path addLineToPoint:point6];
//        [path addArcWithCenter:CGPointMake(cornerRadius, viewHeight-cornerRadius-arrowY) radius:cornerRadius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
//    }
//
//    [path closePath];
//
//    CAShapeLayer *shapeLayer   = [CAShapeLayer layer];
//    shapeLayer = [CAShapeLayer layer];
//    shapeLayer.path            = path.CGPath;
//
//    CAShapeLayer *borderLayer  = [CAShapeLayer layer];
//    borderLayer.path           =   path.CGPath;
//    borderLayer.fillColor      = [UIColor clearColor].CGColor;
//    borderLayer.strokeColor    = [UIColor redColor].CGColor;
//    borderLayer.lineWidth      = 2;
//    borderLayer.frame          =self.bounds;
//    self.layer.mask            = shapeLayer;
//    [self.layer addSublayer:borderLayer];
//
//}


/**
 * 根据字符串计算 CGSize
 */
-(CGSize) getAttributeSizeWithText:(NSString *)text fontSize:(int)fontSize
{
    CGSize size=[text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]}];
    size=[text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]}];
    return size;
}

@end
